
/** 
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package com.tompai.datagather.twitter.acount;

import java.text.ParseException;
import java.time.LocalDateTime;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
import com.tompai.common.DataBaseUtils;
import com.tompai.common.time.TimeTransUtils;
import com.tompai.datagather.common.MybatisUtil;
import com.tompai.datagather.dao.TwitterAcountsMapper;
import com.tompai.datagather.model.TwitterAcounts;
import com.tompai.datagather.twitter.ShadowSockets5Proxy;

/**
 * @desc: datagather-tools
 * @name: TwitterHomeInfo.java
 * @author: tompai
 * @createTime: 2018年5月7日 下午10:56:34
 * @history:
 * @version: v1.0
 */

public class TwitterHomeInfo  {

	public static final Logger LOG = LoggerFactory.getLogger(TwitterHomeInfo.class);
	private static String twitterHome = "https://twitter.com/";

	private String acount;

	
	public String getAcount() {

		return acount;
	}

	public void setAcount(String acount) {

		this.acount = acount;
	}
 
	public boolean parseHomePage(String acount) {

		String html;

		String profileName;
		String profileScreenName;
		String profileUrl;

		String profileBio;
		String profileLocation;
		String birthday;
		String profileJoinDate;

		String profilePhotorail;

		String tweetsNumber = null;
		String followingNumber = null;
		String followersNumber = null;
		String favoritesNumber = null;
		String homepage = twitterHome + acount;

		DataBaseUtils dbUtil=new DataBaseUtils();
		try {
			//=========
			TwitterAcountsMapper acountMapper;
			acountMapper = MybatisUtil.getSession().getMapper(TwitterAcountsMapper.class);
			TwitterAcounts acounts = new TwitterAcounts();
			//=============
			html = ShadowSockets5Proxy.twitterLink(homepage);
			if(StringUtils.isEmpty(html)) {
				LOG.error("Failed home url : "+homepage);
				return false;
			}
			JSONObject json = JSONObject.parseObject(html);
			Document doc = Jsoup.parse(json.getString("page"));
			//LOG.info("Home page html: "+doc.html());
			
			String errorPage=doc.select(".errorpage-body-content").text();
			String searchSeq="Sorry, that page doesn’t exist!";
			if (StringUtils.contains(errorPage, searchSeq)) {
				LOG.error("Page doesn’t exist:"+homepage);
				return false;
			}
			profileName = doc.select(".ProfileHeaderCard-nameLink.u-textInheritColor.js-nav").text();
			profileName=dbUtil.filterEmojiChar(profileName);
			acounts.setProfileName(profileName);

			profileScreenName = doc.select(".ProfileHeaderCard-screennameLink.u-linkComplex.js-nav").text();
			profileScreenName = StringUtils.substring(profileScreenName, 1);
			acounts.setProfileScreenname(profileScreenName);
			if(StringUtils.isEmpty(profileScreenName)) {
				LOG.error("profileScreenName is null!");
				return false;
			}
			//LOG.info("profileScreenName:"+profileScreenName);
			
			profileUrl = doc.select(".ProfileHeaderCard-urlText.u-dir").select("a.u-textUserColor").attr("title");
			acounts.setProfileUrl(profileUrl);
			acounts.setHomepageUrl(twitterHome + profileScreenName);

			profileBio = doc.select(".ProfileHeaderCard-bio.u-dir").text();
			profileBio=dbUtil.filterEmojiChar(profileBio);
			acounts.setProfileBio(profileBio);

			birthday=doc.select(".ProfileHeaderCard-birthdateText.u-dir").text();
			acounts.setProfileBirthday(birthday);
			//LOG.info("birthday :" +birthday);
			
			profileLocation = doc.select(".ProfileHeaderCard-locationText.u-dir").text();
			profileLocation=dbUtil.filterEmojiChar(profileLocation);
			acounts.setProfileLocation(profileLocation);

			//上午9:50 - 2009年8月20日
			String formatType = "h:mm a - dd MMM yyyy";
			//String formatType = "a hh:mm - yyyy MM dd";
			profileJoinDate = doc.select(".ProfileHeaderCard-joinDateText.js-tooltip.u-dir").attr("title");
			//LOG.info("profileJoinDate :" +profileJoinDate);
			try {
				if (StringUtils.isNotEmpty(profileJoinDate)) {
					Date date = TimeTransUtils.stringToDate(profileJoinDate, formatType);
					acounts.setProfileJoindate(date);
				}
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				LOG.error("Parse time error:"+profileJoinDate);
				e.printStackTrace();
			}
			profilePhotorail = doc.select(".PhotoRail-headingWithCount.js-nav").text();
			acounts.setProfilePhotorail(profilePhotorail);

			tweetsNumber = doc.select("li.ProfileNav-item.ProfileNav-item--tweets.is-active")
					.select("a.ProfileNav-stat.ProfileNav-stat--link.u-borderUserColor.u-textCenter.js-tooltip.js-nav")
					.select("span.ProfileNav-value").attr("data-count");
			if (StringUtils.isNoneEmpty(tweetsNumber)) {
				acounts.setTweetNumber(Integer.valueOf(tweetsNumber));
			}

			followingNumber = doc.select("li.ProfileNav-item.ProfileNav-item--following").select(
					"a.ProfileNav-stat.ProfileNav-stat--link.u-borderUserColor.u-textCenter.js-tooltip.js-nav.u-textUserColor")
					.select("span.ProfileNav-value").attr("data-count");
			if (StringUtils.isNoneEmpty(followingNumber)) {
				acounts.setFollowingNumber(Integer.valueOf(followingNumber));
			}

			followersNumber = doc.select("li.ProfileNav-item.ProfileNav-item--followers").select(
					"a.ProfileNav-stat.ProfileNav-stat--link.u-borderUserColor.u-textCenter.js-tooltip.js-nav.u-textUserColor")
					.select("span.ProfileNav-value").attr("data-count");
			if (StringUtils.isNoneEmpty(followersNumber)) {
				acounts.setFollowerNumber(Integer.valueOf(followersNumber));
			}

			favoritesNumber = doc.select("li.ProfileNav-item.ProfileNav-item--favorites").select(
					"a.ProfileNav-stat.ProfileNav-stat--link.u-borderUserColor.u-textCenter.js-tooltip.js-nav.u-textUserColor")
					.select("span.ProfileNav-value").attr("data-count");
			if (StringUtils.isNoneEmpty(favoritesNumber)) {
				acounts.setFavoritesNumber(Integer.valueOf(favoritesNumber));
			}
			
			LocalDateTime datetime = LocalDateTime.now();
			acounts.setCollectTime(TimeTransUtils.localDateTimeToDate(datetime));
			if(StringUtils.isNoneEmpty(acounts.getProfileScreenname())) {
				acountMapper.insert(acounts);
				MybatisUtil.getSession().commit(true);
				LOG.info(acount+": filished homepage.");
			}else {
				LOG.error("Failed get screenName :" +homepage);
			}
			
		} catch (Exception ex) {
			// TODO: handle exception
			ex.printStackTrace();
			LOG.error(ex.getMessage());
		}
		return true;
	}
}
